﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Cosmetic.DataEntity;

namespace Cosmetic.Logic
{
	/// <summary>
	/// blog manager class
	/// </summary>
	public class BlogManager : LogicBase
    {
		/// <summary>
		/// Insert a new Blog into database
		/// </summary>
		/// <param name="aBlog">new Blog</param>
		public void InsertBlog(Blog aBlog)
		{
			m_CosmeticDC.Blogs.InsertOnSubmit(aBlog);
			m_CosmeticDC.SubmitChanges();
		}

		/// <summary>
		/// Delete Blog
		/// </summary>
		/// <param name="aBlog">The Blog want delete</param>
		public void DeleteBlog(Blog aBlog)
		{
			m_CosmeticDC.Blogs.DeleteOnSubmit(aBlog);
			m_CosmeticDC.SubmitChanges();
		}



		/// <summary>
		/// get Blog by blog id
		/// </summary>
		/// <param name="aBlogId">the specify blog id</param>
		/// <returns></returns>
		public Blog GetBlogById(Guid aBlogId)
		{
			List<Blog> theBlogList	= (from theBlog in m_CosmeticDC.Blogs
										   where theBlog.BlogId == aBlogId
										   select theBlog).ToList();

			if (theBlogList.Count == 0)
			{
				return null;
			}
			return theBlogList[0];
		}

		/// <summary>
		/// get all Blog 
		/// </summary>
		/// <returns>blog list</returns>
		public List<Blog> GetAllBlog()
		{
			return m_CosmeticDC.Blogs.ToList();
		}

		/// <summary>
		/// select blog by specify date
		/// </summary>
        /// <param name="aDate">the specify date</param>
        /// <param name="aCategory">the specify date</param>
		/// <returns>blog list</returns>
        public List<Blog> GetBlogByDate(DateTime aDate, Category aCategory)
		{
			return (from theBlog in m_CosmeticDC.Blogs
						where theBlog.CreateTime.Date.Equals(aDate.Date) && theBlog.BlogCategory.Equals(aCategory)
						orderby theBlog.CreateTime descending
						select theBlog).ToList();
		}
		
		/// <summary>
		/// select blog between start date and enddate
		/// </summary>
		/// <param name="aStartDate">the start date</param>
        /// <param name="anEndDate">the end date</param>
        /// <param name="aCategory">the end date</param>
		/// <returns>blog list</returns>
        public List<Blog> GetBlogByDate(DateTime aStartDate, DateTime anEndDate, Category aCategory)
		{
			return (from theBlog in m_CosmeticDC.Blogs
                    where aStartDate <= theBlog.CreateTime.Date && anEndDate >= theBlog.CreateTime.Date && theBlog.BlogCategory == aCategory
						orderby theBlog.CreateTime descending
						select theBlog).ToList();
		}

		/// <summary>
		/// select blog by category
		/// </summary>
        /// <param name="aCategoryId">blog category id</param>
        /// <param name="aCategory">blog category id</param>
		/// <returns>blog list</returns>
        public List<Blog> GetBlogByCategory(Guid aCategoryId, Category aCategory)
		{
			return (from theBlog in m_CosmeticDC.Blogs
                    where theBlog.CategoryId == aCategoryId && theBlog.BlogCategory == aCategory
						orderby theBlog.CreateTime descending
						select theBlog).ToList();
        }

        public List<Blog> GetBlogByCategory(Guid aCategoryId)
        {
            return (from theBlog in m_CosmeticDC.Blogs
                    where theBlog.CategoryId == aCategoryId
                    orderby theBlog.CreateTime descending
                    select theBlog).ToList();
        }

        public List<Blog> GetBlogByBlogCategory(Category aCategory)
        {
            return (from theBlog in m_CosmeticDC.Blogs
                    where theBlog.BlogCategory == aCategory
                    orderby theBlog.CreateTime descending
                    select theBlog).ToList();
        }

		/// <summary>
		/// get blog archies
		/// </summary>
		/// <returns>the blog archies</returns>
		public Dictionary<String, DateTime> GetBlogArchies(Category aCategory)
		{
			IOrderedQueryable<Blog> theBlogList	= m_CosmeticDC.Blogs.Where(theBlog=>theBlog.BlogCategory==aCategory).OrderBy(theBlog=>theBlog.CreateTime);
            Dictionary<String, DateTime> theArchives = new Dictionary<string, DateTime>();
            if (theBlogList.Count() > 0)
            {
                DateTime theOldestDate = theBlogList.Min(theBlog => theBlog.CreateTime).Date;
                theOldestDate = theOldestDate.AddDays(-theOldestDate.Day + 1);
                for (DateTime theDate = DateTime.Today.AddDays(-DateTime.Today.Day + 1); theDate >= theOldestDate; theDate = theDate.AddMonths(-1))
                {
                    String theKey = String.Format("{0}年 {1}月 ({2})",
                                        theDate.Year,
                                        theDate.Month,
                                        theBlogList.Count(theBlog => theDate <= theBlog.CreateTime && theBlog.CreateTime < theDate.AddMonths(1)));
                    theArchives.Add(theKey, theDate);
                }
            }

			return theArchives;
		}


		/// <summary>
		/// Insert a new Blog comment into database
		/// </summary>
		/// <param name="aBlogComment">new Blog comment</param>
		public void InsertBlogComment(BlogComment aBlogComment)
		{
			m_CosmeticDC.BlogComments.InsertOnSubmit(aBlogComment);
			m_CosmeticDC.SubmitChanges();
		}

		/// <summary>
		/// Delete Blog Comment
		/// </summary>
		/// <param name="aBlogComment">The Blog Comment want delete</param>
		public void DeleteBlogComment(BlogComment aBlogComment)
		{
			m_CosmeticDC.BlogComments.DeleteOnSubmit(aBlogComment);
			m_CosmeticDC.SubmitChanges();
		}



		/// <summary>
		/// get Blog comment by blog comment id
		/// </summary>
		/// <param name="aBlogCommentId">the specify blog comment id</param>
		/// <returns></returns>
		public BlogComment GetBlogCommentById(Guid aBlogCommentId)
		{
			List<BlogComment> theBlogCommentList	= (from theBlogComment in m_CosmeticDC.BlogComments
														   where theBlogComment.BlogCommentId == aBlogCommentId
														   select theBlogComment).ToList();

			if (theBlogCommentList.Count == 0)
			{
				return null;
			}
			return theBlogCommentList[0];
		}

		/// <summary>
		/// get blog comment by blog id
		/// </summary>
		/// <param name="aBlogId">blog id</param>
		/// <returns>blog comment list</returns>
		public List<BlogComment> GetBlogCommentByBlog(Guid aBlogId)
		{
			return (from theBlogComment in m_CosmeticDC.BlogComments
						where theBlogComment.BlogId == aBlogId
						orderby theBlogComment.CreateTime descending
						select theBlogComment).ToList();
		}

		/// <summary>
		/// get "aCount" number newest blog comments
		/// </summary>
		/// <param name="aCount">how much comments wanna get</param>
		/// <returns>blog comment list</returns>
		public List<BlogComment> GetBlogCommentByCount(int aCount)
		{
			return (from theBlogComment in m_CosmeticDC.BlogComments
						orderby theBlogComment.CreateTime descending
						select theBlogComment).Take(aCount).ToList();
		}
	}
}
